<?php

declare(strict_types=1);

namespace App\Listener;

use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use App\Event\ExceptionLogEvent;
use App\Model\admin\LogModel;
use Hyperf\HttpServer\Router\Dispatched;


class ExceptionLogListener implements ListenerInterface
{


    public function listen(): array
    {
        return [
            ExceptionLogEvent::class,
        ];
    }

    public function process(object $event)
    {
		$action = request()->getAttribute(Dispatched::class)->handler->callback;
		$tmp = explode('\\',$action[0]);
		
		$content = request()->all();
		if ($content) {
			foreach ($content as $k => $v) {
				if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false) {
					unset($content[$k]);
				}
			}
		}
	
		$data['application_name'] = $tmp[2];
		$data['username'] = session()->get($tmp[2])->username;
		$data['url'] = request()->url();
		$data['ip'] = reaip();
		$data['useragent'] = request()->header('user-agent');
		$data['content'] = json_encode($content,JSON_UNESCAPED_UNICODE);
		$data['errmsg'] = $event->data['msg'];
		$data['create_time'] = time();
		$data['type'] = 3;
		
		LogModel::insert($data);
		
		
    }
}
